package gui;

import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import client.Client;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

public class OrderCancel extends JFrame{

	final public static int DEFAULT_PORT = 5555;	
    private String host = MainPanel.ip; 
	
	private JPanel jPanelOrderCancel = null;
	private JLabel jLabelOrders = null;
	private JButton jButtonCancel = null;
	private JButton jButtonExit = null;
	private JButton jButtonHelp = null;
	
	static DefaultTableModel model = new DefaultTableModel();
	private JLabel jLabelOrderToCancel = null;
	private JTextField jTextFieldCancelNum = null;
	private JLabel jLabelCancel = null;
	private JScrollPane jScrollPaneOrderResaults = null;
	private JTable jTableOrderResaults = null;
	/**
	 * This method initializes 
	 * 
	 */
	public OrderCancel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(1000, 720));
        this.setContentPane(getJPanelOrderCancel());
        this.setTitle("CANCEL ORDER");
			
	}

	/**
	 * This method initializes jPanelOrderCancel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelOrderCancel() {
		if (jPanelOrderCancel == null) {
			jLabelCancel = new JLabel();
			jLabelCancel.setBounds(new Rectangle(556, 475, 318, 54));
			jLabelCancel.setText("ORDER CANCELED");
			jLabelCancel.setVisible(false);
			jLabelOrderToCancel = new JLabel();
			jLabelOrderToCancel.setBounds(new Rectangle(149, 479, 287, 33));
			jLabelOrderToCancel.setText("PLEASE ENTER THE ORDER NUMBER TO CANCEL:");
			ImageIcon orders = new ImageIcon("orders.jpg");
			jLabelOrders = new JLabel();			
			jLabelOrders.setBounds(new Rectangle(284, 13, 392, 170));
			jLabelOrders.setIcon(orders);
			jPanelOrderCancel = new JPanel();
			jPanelOrderCancel.setBackground(Color.cyan);
			jPanelOrderCancel.setLayout(null);
			jPanelOrderCancel.add(jLabelOrders, null);
			jPanelOrderCancel.add(getJButtonCancel(), null);
			jPanelOrderCancel.add(getJButtonExit(), null);
			jPanelOrderCancel.add(getJButtonHelp(), null);
			jPanelOrderCancel.add(jLabelOrderToCancel, null);
			jPanelOrderCancel.add(getJTextFieldCancelNum(), null);
			jPanelOrderCancel.add(jLabelCancel, null);
			jPanelOrderCancel.add(getJScrollPaneOrderResaults(), null);
		}
		return jPanelOrderCancel;
	}

	/**
	 * This method initializes jButtonCancel takes the order num that was inputted by the user and disables it and credit the users account
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			ImageIcon cancel = new ImageIcon("cancel.png");
			jButtonCancel.setBounds(new Rectangle(404, 531, 128, 131));
			jButtonCancel.setIcon(cancel);
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
					Calendar cal = Calendar.getInstance();
					String CurrentTime=dateFormat.format(cal.getTime());
					
					String[] Cancelorder = new String[3];
					Client CancelOrders = new Client(host,DEFAULT_PORT);
					Cancelorder[0]="cancel_order";
					Cancelorder[1]=jTextFieldCancelNum.getText();					
					Cancelorder[2]=MainPanel.Userinfo[3];
					            
					CancelOrders.accept(Cancelorder);
					String[] OrderInfo = new String[2];
					Object p = CancelOrders.getMsg();
					OrderInfo = (String[])p;	
					if (OrderInfo[0].equalsIgnoreCase("no_entery"))
					{
						ErrorFrame EFrame = new ErrorFrame();
						EFrame.jLabelMassage.setText("THERE IS NO SUCH ORDER");
						EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						EFrame.setSize( 480,217 ); // set frame size
						EFrame.setResizable(true);
						EFrame.setVisible( true );
					}
					else
					{
					String OrderTime=OrderInfo[1];
					int orderprice=Integer.parseInt(OrderInfo[2].trim());
					
					String OrderMinuteS=OrderTime.substring(14, 16);
					String OrderHourS=OrderTime.substring(11, 13);
					String OrderMonthS=OrderTime.substring(5	, 7);
					String OrderDayS=OrderTime.substring(8, 10);
					String CurrentMinuteS=CurrentTime.substring(14, 16);
					String CurrentHourS=CurrentTime.substring(11, 13);
					String CurrentMonthS=CurrentTime.substring(5	, 7);
					String CurrentDayS=CurrentTime.substring(8, 10);
					
					int OrderMinute = Integer.parseInt(OrderMinuteS.trim());
					int OrderHour = Integer.parseInt(OrderHourS.trim());
					int OrderMonth = Integer.parseInt(OrderMinuteS.trim());
					int OrderDay = Integer.parseInt(OrderDayS.trim());
					int CurrentMinute = Integer.parseInt(CurrentMinuteS.trim());
					int CurrentHour = Integer.parseInt(CurrentHourS.trim());
					int CurrentMonth = Integer.parseInt(CurrentMonthS.trim());
					int CurrentDay = Integer.parseInt(CurrentDayS.trim());
					
					if ( ((CurrentMonth>=OrderMonth) || (CurrentDay>=OrderDay)) && ((CurrentHour-OrderHour)>=3))
					{
						jLabelCancel.setVisible(true);
						if (OrderInfo[0].equals("cash"))
						{
							ErrorFrame EFrame = new ErrorFrame();
							EFrame.jLabelMassage.setText("PLEASE STEP OVER TO THE COUNTER");
							EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							EFrame.setSize( 480,217 ); // set frame size
							EFrame.setResizable(true);
							EFrame.setVisible( true ); 
						}
						if (OrderInfo[0].equals("subsription"))
						{
							String[] ChkBalance = new String[2];
							int balnce=0;
							ChkBalance[0]="check_balance";
							ChkBalance[1]=MainPanel.Userinfo[3];
							CancelOrders.accept(ChkBalance);
							Object p1 = CancelOrders.getMsg();
							balnce = (Integer)p1;
							balnce=balnce+orderprice;
							String[] newbalance = new String[3];
							newbalance[0]="updatebalance";						
							newbalance[1]=Integer.toString(balnce);
							newbalance[2]=MainPanel.Userinfo[3];							
							CancelOrders.accept(newbalance);
						}
						if (OrderInfo[0].equals("old_credit_card"))
						{
							ErrorFrame EFrame = new ErrorFrame();
							EFrame.jLabelMassage.setText("YOUR CREDIT CARD WILL BE CREDITED");
							EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							EFrame.setSize( 480,217 ); // set frame size
							EFrame.setResizable(true);
							EFrame.setVisible( true ); 
						}
						if (OrderInfo[0].equals("new_credit_card"))
						{
							ErrorFrame EFrame = new ErrorFrame();
							EFrame.jLabelMassage.setText("YOUR CREDIT CARD WILL BE CREDITED");
							EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							EFrame.setSize( 480,217 ); // set frame size
							EFrame.setResizable(true);
							EFrame.setVisible( true ); 
						}
					}
										
					
					if ( (CurrentMonthS.equals(OrderMonthS)) && (CurrentDayS.equals(OrderDayS)) && (((CurrentHour-OrderHour)==2) || ((CurrentHour-OrderHour)==3)))
					{						
						jLabelCancel.setVisible(true);
						ErrorFrame EFrame = new ErrorFrame();
						EFrame.jLabelMassage.setText("CANCELATION FEE: ORDER PRICE-"+orderprice);
						EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						EFrame.setSize( 480,217 ); // set frame size
						EFrame.setResizable(true);
						EFrame.setVisible( true ); 
					}
					
					if ( (CurrentMonthS.equals(OrderMonthS)) && (CurrentDayS.equals(OrderDayS)) && ((CurrentHour-OrderHour)<=2))	
					{
						
						jLabelCancel.setVisible(true);
						if (OrderInfo[0].equals("subsription"))
						{
							String[] ChkBalance = new String[2];
							int balnce=0;
							ChkBalance[0]="check_balance";
							ChkBalance[1]=MainPanel.Userinfo[3];
							CancelOrders.accept(ChkBalance);
							Object p1 = CancelOrders.getMsg();
							balnce = (Integer)p1;
							balnce=balnce+(orderprice/2);
							String[] newbalance = new String[3];
							newbalance[0]="updatebalance";						
							newbalance[1]=Integer.toString(balnce);
							newbalance[2]=MainPanel.Userinfo[3];							
							CancelOrders.accept(newbalance);
							
						ErrorFrame EFrame = new ErrorFrame();
						EFrame.jLabelMassage.setText("CANCELATION FEE: 50% OF THE ORDER PRICE- "+(orderprice/2)+" NIS");
						EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						EFrame.setSize( 480,217 ); // set frame size
						EFrame.setResizable(true);
						EFrame.setVisible( true );
						}
						if (OrderInfo[0].equals("old_credit_card"))
						{
						ErrorFrame EFrame = new ErrorFrame();
						EFrame.jLabelMassage.setText("CANCELATION FEE: 50% OF THE ORDER PRICE- "+(orderprice/2)+" NIS");
						EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						EFrame.setSize( 480,217 ); // set frame size
						EFrame.setResizable(true);
						EFrame.setVisible( true );
						}
						if (OrderInfo[0].equals("new_credit_card"))
						{						
						ErrorFrame EFrame = new ErrorFrame();
						EFrame.jLabelMassage.setText("CANCELATION FEE: 50% OF THE ORDER PRICE- "+(orderprice/2)+" NIS");
						EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						EFrame.setSize( 480,217 ); // set frame size
						EFrame.setResizable(true);
						EFrame.setVisible( true );
						}
					}	
					
					for( int i = model.getRowCount() - 1; i >= 0; i-- )
					{
						String temp1 = (String) model.getValueAt(i, 0);
						if (temp1.equals(Cancelorder[1]))	
							model.removeRow(i);
					}
						
					
					}	
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jButtonExit and closes this window
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonExit() {
		if (jButtonExit == null) {
			jButtonExit = new JButton();
			jButtonExit.setBounds(new Rectangle(819, 593, 115, 49));
			jButtonExit.setText("EXIT");
			jButtonExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					for( int i = model.getRowCount() - 1; i >= 0; i-- )					
						model.removeRow(i);					
					OrderCancel.this.dispose();
				}
			});
		}
		return jButtonExit;
	}

	/**
	 * This method initializes jButtonHelp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonHelp() {
		if (jButtonHelp == null) {
			jButtonHelp = new JButton();
			jButtonHelp.setBounds(new Rectangle(29, 594, 125, 50));
			jButtonHelp.setText("HELP");
			jButtonHelp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					HelpFrame HFrame = new HelpFrame();

					HFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					HFrame.setSize( 600,720 ); // set frame size
					HFrame.setResizable(false);
					HFrame.setVisible( true ); // display frame
				}
			});
		}
		return jButtonHelp;
	}

	/**
	 * This method initializes jTextFieldCancelNum	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCancelNum() {
		if (jTextFieldCancelNum == null) {
			jTextFieldCancelNum = new JTextField();
			jTextFieldCancelNum.setBounds(new Rectangle(440, 479, 101, 33));
		}
		return jTextFieldCancelNum;
	}

	/**
	 * This method initializes jScrollPaneOrderResaults	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneOrderResaults() {
		if (jScrollPaneOrderResaults == null) {
			jScrollPaneOrderResaults = new JScrollPane();
			jScrollPaneOrderResaults.setBounds(new Rectangle(45, 190, 917, 278));
			jScrollPaneOrderResaults.setViewportView(getJTableOrderResaults());
		}
		return jScrollPaneOrderResaults;
	}

	/**
	 * This method initializes jTableOrderResaults	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableOrderResaults() {
		if (jTableOrderResaults == null) {
			jTableOrderResaults = new JTable(model);
			model.addColumn("ORDER_NUM");
			model.addColumn("ITEM_NUM");
			model.addColumn("SUM");
			model.addColumn("ORDER_DATE");
			jTableOrderResaults.setBounds(new Rectangle(52, 217, 856, 243));
		}
		return jTableOrderResaults;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
